#import "ImageMediator.h"
#import "DynamicEntity.h"

static ImageMediator* imgMediator;

@implementation ImageMediator
@synthesize photos;

+(ImageMediator*)getInstanse{
    if (imgMediator == nil) {
        imgMediator = [[ImageMediator alloc]init];
    }
    return imgMediator;
}

-(void) initDynamicViewController:(DynamicViewController*) vc{
    self.dynamic_view_controller = vc;
}

//imgIdx = 7 代表的时单张大图
-(void) cellAtIndex:(NSInteger)cellIdx imageAtIndex:(int) imgIdx{
    //参数组织
    int cell_index = (int)cellIdx;
    NSString *img1 = [NSString stringWithFormat:@"image%d",1];
    NSString *img2 = [NSString stringWithFormat:@"image%d",2];
    NSString *img3 = [NSString stringWithFormat:@"image%d",3];
    NSString *img4 = [NSString stringWithFormat:@"image%d",4];
    NSString *img5 = [NSString stringWithFormat:@"image%d",5];
    NSString *img6 = [NSString stringWithFormat:@"image%d",6];
    NSString *img7 = [NSString stringWithFormat:@"image%d",7];
    NSString *img8 = [NSString stringWithFormat:@"image%d",8];
    NSString *img9 = [NSString stringWithFormat:@"image%d",9];
    NSString *img10 = [NSString stringWithFormat:@"image%d",10];
    
    DynamicEntity* entity = [DynamicEntity getInstanse];
    NSString *img1URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img1];
    NSString *img2URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img2];
    NSString *img3URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img3];
    NSString *img4URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img4];
    NSString *img5URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img5];
    NSString *img6URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img6];
    NSString *img7URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img7];
    NSString *img8URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img8];
    NSString *img9URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img9];
    NSString *img10URL = [(NSDictionary*)[entity.deArray objectAtIndex:cell_index] objectForKey:img10];
    // Create array of MWPhoto objects
    photos = [NSMutableArray array];
    
    if (img1URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img1URL]]];
    }
    if (img2URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img2URL]]];
    }
    if (img3URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img3URL]]];
    }
    if (img4URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img4URL]]];
    }
    if (img5URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img5URL]]];
    }
    if (img6URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img6URL]]];
    }
    if (img7URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img7URL]]];
    }
    if (img8URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img8URL]]];
    }
    if (img9URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img9URL]]];
    }
    if (img10URL.length>4) {
        [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:img10URL]]];
    }
    
//    [photos addObject:[MWPhoto photoWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"wel2" ofType:@"png"]]]];
    
    // Create browser (must be done each time photo browser is
    // displayed. Photo browser objects cannot be re-used)
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self.dynamic_view_controller];
    
    // Set options
    browser.displayActionButton = NO; // Show action button to allow sharing, copying, etc (defaults to YES)
    browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    //    browser.wantsFullScreenLayout = YES; // iOS 5 & 6 only: Decide if you want the photo browser full screen, i.e. whether the status bar is affected (defaults to YES)
    
    // Optionally set the current visible photo before displaying
    [browser setCurrentPhotoIndex:(imgIdx-1)];
    
    // Present
//    [self.dynamic_view_controller.navigationController pushViewController:browser animated:YES];
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.dynamic_view_controller presentViewController:nc animated:YES completion:nil];
    
    // Manipulate
    //    [browser showNextPhotoAnimated:YES];
    //    [browser showPreviousPhotoAnimated:YES];
}

@end
